<?php


namespace App\Repositories\Ota\Tc\Policy\Eloquent;


use App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface;
use App\Repositories\Ota\Tc\Policy\Service\TcPolicyService;
use App\Repositories\Policy\Eloquent\TcRepository;
use Curl\StrService\StrService;

class TcPolicyRepository extends TcRepository implements TcPolicyInterface
{

    /**
     * 添加特殊政策
     * @param $param
     * @return mixed|void
     */
    public function addSpecial($param){

        /*$this->formatData($param);
        $url = 'specialpolicy/addspecialpolicy/';

        return $this->req($url,array_merge($this->messageHead,$this->messageBody));*/


        TcPolicyService::formatData($param, $this);

        $url = '/Policy/Special/Add?t='.time();
        $param = [
            'messageHead'=>[
                'userName'      => $this->app_key,
                'requestGuid'   => $param[0]['ext'],
                'callbackUrl'   => $param[0]['url']
            ],
            'messageBody'   => $this->messageBody['messageBody'],
        ];

        $this->setHeader($url,$param);

        //dd($this->httpsRequest($url,$param));
        return $this->httpsRequest($url,$param);

    }

    /**
     * 删除政策代码
     * @param $delType
     * @param int $isFuzzy
     * @return mixed
     */
    public function deleteSpecial($policyCode, $ext = '', $callbackUrl = '', $isFuzzy=1, $delType=2)
    {


        $this->messageBody['messageBody']= [
            'isFuzzyMatch'=>$isFuzzy,
            'delType'=>$delType
        ];

        if ($delType == 2) {
            $this->messageBody['messageBody']['policyCodes'] = [$policyCode];
        } else {
            $this->messageBody['messageBody']['policyIds'] = [$policyCode];
        }

        $this->messageBody['messageBody'] = base64_encode(gzencode(json_encode($this->messageBody["messageBody"])));

        $url = '/Policy/Special/Delete?t='.time();
        $param = [
            'messageHead'=>[
                'userName'      => $this->app_key,
                'requestGuid'   => empty($ext) ? StrService::uuid() : $ext,
                'callbackUrl'   => $callbackUrl
            ],
            'messageBody'   => $this->messageBody['messageBody'],
        ];

        $this->setHeader($url,$param);

        return $this->httpsRequest($url,$param);

    }

    /**
     * 同程政策列表
     * @param $param
     * @return mixed
     */
    public function policyLists($param){

        $time = time();
        $url = '/Policy/Special/Query?t='.$time;

        $param = [
            'messageHead'=>[
                'userName'      => $this->app_key,
                'requestGuid'   => StrService::uuid()
            ],
            'messageBody'   => [
                'pageIndex'     => $param['page'],
                'pageSize'      => $param['page_size'],
                'airCompany'    => $param['hs_code'],
                'policyCode'    => $param['policy_code'],
                "isValid"       => 1,
            ],
        ];

        //设置header
        $this->setHeader($url,$param);

        $res = $this->httpsRequest($url,$param);

        if ($res  && $res['errorCode'] == 0 && isset($res['data'])) {

            return json_decode(gzdecode(base64_decode($res['data'])),true);
        }

        return false;
    }

    /**
     * 修改同程政策
     * @param $policyList
     * @return void
     */
    public function updatePolicyById($param){

        $policyList = TcPolicyService::formatUpdateData($param);

        $this->messageBody["messageBody"] = [
            'policyList' => $policyList
        ];

        $this->messageBody["messageBody"] = base64_encode(gzencode(json_encode($this->messageBody["messageBody"],JSON_UNESCAPED_UNICODE)));

        $param = [
            'messageHead'     =>  [
                'userName'    => 'hnhb',
                "requestGuid" => $param[0]['ext'],
                'callbackUrl' => $param[0]['url']
            ],
            'messageBody'   =>  $this->messageBody["messageBody"],
        ];

        $time = time();
        $url = '/Policy/Special/Update?t='.$time;

        //设置header
        $this->setHeader($url,$param);

        return $this->httpsRequest($url,$param);

    }

    /**
     * 修改同程政策
     * @param $param
     * @return void
     */
    public function oldUpdatePolicyById($param){

        $this->messageBody["messageBody"] = [
            'policyList' => [
                [
                    "policyCode"    => $param['policy_code'],
                    "keepMoney"     =>  $param['min_price'],
                ]
            ]
        ];
        var_dump($param);
        $this->messageBody["messageBody"] = base64_encode(gzencode(json_encode($this->messageBody["messageBody"],JSON_UNESCAPED_UNICODE)));

        $param2 = [
            'messageHead'   =>  $this->messageHead['messageHead'],
            'messageBody'   =>  $this->messageBody["messageBody"],
        ];


        return $this->req('specialpolicy/updatespecialpolicy/',$param2);

    }


    /**
     * 获取政策详情
     * @param $policyId
     * @return mixed
     */
    public function getPolicyInfoById($policyId){

        $time = time();
        $url = '/Policy/Special/Detail?t='.$time;

        $param = [
            'messageHead'=>$this->messageHead['messageHead'],
            'messageBody'   => [
                'policyId' => [
                    $policyId
                ]
            ],
        ];


        //设置header
        $this->setHeader($url,$param);

        $res = $this->httpsRequest($url,$param);

        if ($res  && $res['errorCode'] == 0 && isset($res['data'])) {

            return json_decode(gzdecode(base64_decode($res['data'])),true);
        }

        return false;
    }

    /**
     * 查询低价
     * @param $param
     * @return mixed
     */
    public function lowPricePolicyList($param){


        $url = '/Parity/Oneway/Query?t='.time();
        $params = [
            'messageHead' => [
                'userName'      => $this->app_key,
                'requestGuid'   => StrService::uuid()
            ],
            'messageBody' => $param,
        ];

        $this->setHeader($url,$params);

        $res = $this->httpsRequest($url,$params);


        if ($res  && $res['errorCode'] == 0 && isset($res['data'])) {

            return json_decode(gzdecode(base64_decode($res['data'])),true);
        }

        return false;

    }
}